<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Google Aviator 表达式测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
        }
        input, textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 14px;
            box-sizing: border-box;
        }
        button {
            background-color: #007bff;
            color: white;
            padding: 12px 24px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            margin-right: 10px;
            margin-bottom: 10px;
        }
        button:hover {
            background-color: #0056b3;
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            border-radius: 5px;
            font-family: monospace;
        }
        .success {
            background-color: #d4edda;
            border: 1px solid #c3e6cb;
            color: #155724;
        }
        .error {
            background-color: #f8d7da;
            border: 1px solid #f5c6cb;
            color: #721c24;
        }
        .info {
            background-color: #d1ecf1;
            border: 1px solid #bee5eb;
            color: #0c5460;
        }
        .example {
            background-color: #fff3cd;
            border: 1px solid #ffeaa7;
            padding: 15px;
            border-radius: 5px;
            margin: 20px 0;
        }
        .example h3 {
            margin-top: 0;
            color: #856404;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Google Aviator 表达式测试</h1>
        
        <div class="example">
            <h3>默认表达式示例</h3>
            <p><strong>表达式：</strong><code>name == 'lujieni' && age > 30</code></p>
            <p><strong>说明：</strong>用户名必须是 'lujieni' 且年龄大于30才匹配</p>
        </div>

        <div class="form-group">
            <label for="name">姓名:</label>
            <input type="text" id="name" value="lujieni" placeholder="请输入姓名">
        </div>

        <div class="form-group">
            <label for="age">年龄:</label>
            <input type="number" id="age" value="35" placeholder="请输入年龄">
        </div>

        <div class="form-group">
            <label for="expression">自定义表达式 (可选):</label>
            <textarea id="expression" rows="3" placeholder="留空使用默认表达式，或输入自定义表达式如: age >= 25 && name != null"></textarea>
        </div>

        <button onclick="checkWithDefault()">使用默认表达式检查</button>
        <button onclick="checkWithCustom()">使用自定义表达式检查</button>
        <button onclick="validateExpression()">验证表达式语法</button>
        <button onclick="getDefaultExpression()">获取默认表达式</button>

        <div id="result"></div>
    </div>

    <script>
        const API_BASE = '/aviator';

        function showResult(data, type = 'info') {
            const resultDiv = document.getElementById('result');
            resultDiv.className = `result ${type}`;
            resultDiv.innerHTML = '<pre>' + JSON.stringify(data, null, 2) + '</pre>';
        }

        function showError(message) {
            showResult({error: message}, 'error');
        }

        async function checkWithDefault() {
            const name = document.getElementById('name').value;
            const age = parseInt(document.getElementById('age').value);

            if (!name || isNaN(age)) {
                showError('请输入有效的姓名和年龄');
                return;
            }

            try {
                const response = await fetch(API_BASE + '/check', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({name, age})
                });

                const data = await response.json();
                showResult(data, data.match ? 'success' : 'info');
            } catch (error) {
                showError('请求失败: ' + error.message);
            }
        }

        async function checkWithCustom() {
            const name = document.getElementById('name').value;
            const age = parseInt(document.getElementById('age').value);
            const expression = document.getElementById('expression').value.trim();

            if (!name || isNaN(age)) {
                showError('请输入有效的姓名和年龄');
                return;
            }

            if (!expression) {
                showError('请输入自定义表达式');
                return;
            }

            try {
                const response = await fetch(API_BASE + '/check-custom?expression=' + encodeURIComponent(expression), {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({name, age})
                });

                const data = await response.json();
                if (data.error) {
                    showResult(data, 'error');
                } else {
                    showResult(data, data.match ? 'success' : 'info');
                }
            } catch (error) {
                showError('请求失败: ' + error.message);
            }
        }

        async function validateExpression() {
            const expression = document.getElementById('expression').value.trim();

            if (!expression) {
                showError('请输入要验证的表达式');
                return;
            }

            try {
                const response = await fetch(API_BASE + '/validate?expression=' + encodeURIComponent(expression), {
                    method: 'POST'
                });

                const data = await response.json();
                showResult(data, data.valid ? 'success' : 'error');
            } catch (error) {
                showError('请求失败: ' + error.message);
            }
        }

        async function getDefaultExpression() {
            try {
                const response = await fetch(API_BASE + '/default-expression');
                const data = await response.json();
                showResult(data, 'info');
            } catch (error) {
                showError('请求失败: ' + error.message);
            }
        }
    </script>
</body>
</html>